package com.dfrz.dao;/*
 * @创建者：黄苟苟
 * @创建时间：2021/6/30   10:22
 * @Description描述
 **/

import com.dfrz.entry.User;
import com.dfrz.util.JdbcUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 86177
 */
public class UserDao {
    /*根据用户名登录*/
    public User login(String loginName, String passWord) {
        User userinfo = null;
        String sqlStr = "select * from userinfo where loginName=? and passWord=? ";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            userinfo = queryRunner.query(sqlStr,new BeanHandler <User>(User.class),loginName,passWord);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return userinfo;
    }
    /*判断字段是否重复*/
    public User queryUserByType(String val,Integer type){
        User userinfo =null;
       String sqlStr="";
        if (type==1){
            sqlStr="select * from userinfo where loginName=?";
        }else if (type==2){
            sqlStr="select *from userinfo where phone=?";
        }else{
            sqlStr="select * from userinfo where eMail=?";
        }
       QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            userinfo = queryRunner.query(sqlStr,new BeanHandler<User>(User.class),val);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return userinfo;
    }
    /*添加用户*/
    public int insertUser(User userinfo){
         int count = 0;
         String sqlStr="insert into userinfo(name,loginName,passWord,gender,age,phone,eMail,headerURL)" +
                 " values(?,?,?,?,?,?,?,?)";
         QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,userinfo.getName(),userinfo.getLoginName(),
                    userinfo.getPassWord(),userinfo.getGender(),userinfo.getAge(),
                    userinfo.getPhone(),userinfo.geteMail(),userinfo.getHeaderURL());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }
    /*更新用户*/
    public int updateUser(User userinfo){
        int count = 0;
        String sqlStr="update userinfo set name=?, loginName=?,passWord=?,gender=?,age=?,phone=?," +
                "eMail=?,headerURL=? where id=?";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,sqlStr,userinfo.getName(),userinfo.getLoginName(),
                    userinfo.getPassWord(),userinfo.getGender(),userinfo.getAge(),
                    userinfo.getPhone(),userinfo.geteMail(),userinfo.getHeaderURL());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
    /*逻辑删除用户*/
public int deleteUserById(Integer id){
    int count = 0;
    String sqlStr="update userinfo  set status=1 where id=?";
    QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
    try {
        count=queryRunner.update(sqlStr,id);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return count;
}
    public List<User> queryUserByPage(String name, int limit, int startNum) {
        List<User> listUser = null;
        String str = "";
        if (name!=null||!"".equals(name)) {
            str = "select * from userinfo where name like concat('%',?,'%') and status=1 order by id desc limit ?,?";

            QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
            try {
                listUser = queryRunner.query(str, new BeanListHandler<User>(User.class), name, startNum, limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else {
            str = "select * from userinfo where  status=1 order by id desc limit ?,?";
            QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
            try {
                listUser = queryRunner.query(str, new BeanListHandler<User>(User.class), startNum, limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return listUser;
    }


    /*测试环节*/
    public static void main(String[] args) {
        UserDao userinfoDao=new UserDao();
        User userinfo = userinfoDao.login("黄苟苟","123456");
        System.out.println(userinfo);

    }

}
